<template>
  <div>
    <el-card>
      <el-alert title="消息提示的文案" type="info" show-icon center> </el-alert>
      <!-- 步骤条 -->
      <el-steps
        :space="200"
        finish-status="success"
        :align-center="true"
        :active="activIndex - 0"
      >
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <!-- tab栏 -->
      <el-form
        :model="addForm"
        :rules="rules"
        ref="addruleForm"
        label-width="100px"
        label-position="top"
      >
        <el-tabs
          :tab-position="'left'"
          style="height: 600px"
          v-model="activIndex"
          @tab-click="tabclick"
        >
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="addForm.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格(元)" prop="goods_price">
              <el-input type="number" v-model="addForm.goods_price"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input v-model="addForm.goods_weight"></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input type="number" v-model="addForm.goods_number"></el-input>
            </el-form-item>
            <!-- 商品分类 -->
            <el-form-item label="商品分类" prop="goods_cat">
              <el-cascader
                v-model="addForm.goods_cat"
                :options="catelist"
                :props="cateProps"
                @change="handleChange"
              ></el-cascader>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品参数" name="1">
            <!-- 渲染表单的item -->
            <el-form-item
              :label="item.attr_name"
              v-for="item in manyTable"
              :key="item.attr_id"
            >
              <!-- 复选框 -->
              <el-checkbox-group v-model="item.tags">
                <el-checkbox
                  :label="cb"
                  v-for="(cb, i) in item.tags"
                  :key="i"
                  border
                ></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品属性" name="2">
            <el-form-item
              :label="item.attr_name"
              v-for="item in onlyTable"
              :key="item.attr_id"
            >
              <el-input v-model="item.attr_vals"></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="3">
            <!-- action 图片要上传的后台的api -->
            <el-upload :action="uploadURL" list-type="picture">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="4">
            <!-- 富文本编辑器主件 -->
            <quill-editor v-model="addForm.goods_introduce"></quill-editor>
            <el-button type="primary" class="btn" @click="add"
              >添加商品</el-button
            >
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import _ from "lodash";
import { Goodscate, getParamsList, getNewGoods } from "@/api/user";
import { Message } from "element-ui";
export default {
  data() {
    return {
      activIndex: "0",
      many: "many",
      only: "only",
      //动态参数列表数据
      manyTable: [],
      //静态属性列表
      onlyTable: [],
      // 添加商品的表单对象
      addForm: {
        goods_name: "",
        goods_price: "",
        goods_weight: "",
        goods_number: "",
        //商品所属的分类数组
        goods_cat: [],
        //商品的详情描述
        goods_introduce: "",
      },
      rules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
        goods_cat: [
          { required: true, message: "请选择商品分类", trigger: "blur" },
        ],
      },
      addruleForm: {},
      //商品分类列表
      catelist: [],
      cateProps: {
        label: "cat_name",
        value: "cat_id",
        children: "children",
      },
      //地址
      uploadURL: "http://9yuecloud.com:9988/api/private/v1",
      attrs: [],
    };
  },
  created() {
    this.getCateLlist();
  },
  methods: {
    //添加商品
    async add() {
      //1.将addfrom表单复制一个，避免影响原有的双向绑定
      let form = JSON.parse(JSON.stringify(this.addForm)); //2.处理goods_cat,将其有字符串转换为数组
      form.goods_cat = form.goods_cat.join(","); //3.处理动态参数
      let attrsArray = [];
      this.manyTable.forEach((item) => {
        const newInfo = {
          attr_id: item.attr_id,
          attr_value: item.attr_vals,
        };
        attrsArray.push(newInfo);
      }); //4.处理静态属性
      this.onlyTable.forEach((item) => {
        const newInfo = { attr_id: item.attr_id, attr_value: item.attr_vals };
        attrsArray.push(newInfo);
      });
      form["attrs"] = attrsArray; //将参数数组赋值到新属性attrs上
      console.log(form); //5.将商品信息表单对象通过API提交给后台

      const res = await getNewGoods(form);
      console.log(res);
      Message.success("添加成功");
      this.$router.push("/goods");
    },
    async tabclick() {
      //证明访问的是动态参数列表
      if (this.activIndex === "1") {
        const data = await getParamsList(this.cateId, this.many);
        console.log(data);

        data.forEach((item) => {
          item.tags =
            item.attr_vals.length === 0 ? [] : item.attr_vals.split(" ");
        });
        this.manyTable = data;
      } else if (this.activIndex === "2") {
        const data = await getParamsList(this.cateId, this.only);
        // console.log(data);

        data.forEach((item) => {
          item.tags =
            item.attr_vals.length === 0 ? [] : item.attr_vals.split(" ");
        });
        this.onlyTable = data;
      }
    },
    //级联选择器选中项变化，会触发这个函数
    handleChange() {
      if (this.addForm.goods_cat.length !== 3) {
        this.addForm.goods_cat = [];
      }
    },
    //获取所有商品分类数据
    async getCateLlist() {
      try {
        const data = await Goodscate();
        // console.log(data);
        this.catelist = data;
        // console.log(this.catelist);
      } catch (error) {}
    },
  },
  computed: {
    cateId() {
      if (this.addForm.goods_cat.length === 3) {
        return this.addForm.goods_cat[2];
      }
      return null;
    },
  },
};
</script>

<style scoped >
.el-steps {
  margin: 15px 0;
}
.el-checkbox {
  margin: 0 5px 0 !important;
}
.btn {
  margin-top: 15px;
}
</style>
